package org.tp8.sampleapp.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.tp8.sampleapp.jaxb.adapter.XmlLongAdapter;

/**
 * The Class Teacher.
 */
@Entity
@Table(name = "teacher")
@XmlRootElement(name = "teacher")
@XmlAccessorType(XmlAccessType.FIELD)
public class Teacher extends AbstractModel<Long> {

	/**
	 * The Constant serialVersionUID.
	 */
	private static final long serialVersionUID = 7156556284934784064L;

	/**
	 * The id.
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	@XmlElement(name = "id")
	@XmlID
	@XmlJavaTypeAdapter(XmlLongAdapter.class)
	private Long id;

	/**
	 * The first name.
	 */
	@Column(name = "first_name")
	@XmlElement(name = "firstName")
	private String firstName;

	/**
	 * The last name.
	 */
	@Column(name = "last_name")
	@XmlElement(name = "lastName")
	private String lastName;

	/**
	 * The course.
	 */
	@OneToMany(mappedBy = "teacher", fetch = FetchType.EAGER)
	@XmlElement(name = "teacher")
	@XmlIDREF
	private List<Course> courses;

	/**
	 * Gets the course.
	 * 
	 * @return the course
	 */
	public List<Course> getCourses() {
		return this.courses;
	}

	/**
	 * Gets the first name.
	 * 
	 * @return the first name
	 */
	public String getFirstName() {
		return this.firstName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tp8.sampleapp.model.BaseModel#getId()
	 */
	@Override
	public Long getId() {
		return this.id;
	}

	/**
	 * Gets the last name.
	 * 
	 * @return the last name
	 */
	public String getLastName() {
		return this.lastName;
	}

	/**
	 * Sets the course.
	 * 
	 * @param course
	 *            the new course
	 */
	public void setCourses(final List<Course> courses) {
		this.courses = courses;
	}

	/**
	 * Sets the first name.
	 * 
	 * @param firstName
	 *            the new first name
	 */
	public void setFirstName(final String firstName) {
		this.firstName = firstName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tp8.sampleapp.model.BaseModel#setId(java.lang.Object)
	 */
	@Override
	public void setId(final Long id) {
		this.id = id;
	}

	/**
	 * Sets the last name.
	 * 
	 * @param lastName
	 *            the new last name
	 */
	public void setLastName(final String lastName) {
		this.lastName = lastName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((lastName == null) ? 0 : lastName.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (obj instanceof Long) {
			Long id = (Long) obj;
			return id.equals(getId());
		}
		if (!(obj instanceof Teacher)) {
			return false;
		}
		Teacher other = (Teacher) obj;
		if (firstName == null) {
			if (other.firstName != null) {
				return false;
			}
		} else if (!firstName.equals(other.firstName)) {
			return false;
		}
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (lastName == null) {
			if (other.lastName != null) {
				return false;
			}
		} else if (!lastName.equals(other.lastName)) {
			return false;
		}
		return true;
	}

}
